<?php

namespace App\Kuafu\Product\Model;

use App\Kuafu\Base\Model\Traits\KfSoftDeletes;
use App\Kuafu\Base\Model\Traits\KfUid;
use App\Kuafu\Category\Model\CategoryModel;
use App\Kuafu\Product\Enum\CodeEnum;
use App\Kuafu\Product\Enum\ProductCacheEnum;
use App\Kuafu\Product\Enum\ProductCodeEnum;
use App\Kuafu\Product\Exceptions\ProductException;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Kuafu\Base\Enum\DBEnum;
use App\Kuafu\Base\Model\BaseModel;

class PackageProductMapping extends BaseModel
{
    use HasFactory;

    public const CREATED_AT = null;
    public const UPDATED_AT = null;

    protected $fillable    = [
        'id',
        'packageId',
        'menuId',
        'productId',
        'num',
    ];
    protected $modelCached = false;

    /**
     * 添加套餐关联商品
     * @param $data
     * @throws ProductException
     */
    public static function addPackageProduct($data): bool
    {
        try {
            return self::query()->insert($data);
        } catch (\Exception $exception) {
            throw new ProductException(ProductCodeEnum::ERR_PRODUCT_PACKAGE_INSERT, '', $exception);
        }
    }

    public static function setPackageProductById($packageId, $menuId, $data)
    {
        try {
            return self::query()
                       ->where('packageId', $packageId)
                       ->where('menuId', $menuId)
                       ->update($data);
        } catch (\Exception $exception) {
            throw new ProductException(ProductCodeEnum::ERR_PRODUCT_PACKAGE_DELETED, '', $exception);
        }
    }


    public static function delPackageProductById($packageId, $menuId): int
    {
        try {
            return self::query()
                       ->where('packageId', $packageId)
                       ->where('menuId', $menuId)
                       ->delete();
        } catch (\Exception $exception) {
            throw new ProductException(ProductCodeEnum::ERR_PRODUCT_PACKAGE_DELETED, '', $exception);
        }
    }

    public static function delPackageProductByIds($packageIds, $menuId)
    {
        try {
            return self::query()
                       ->whereIn('packageId', $packageIds)
                       ->where('menuId', $menuId)
                       ->delete();
        } catch (\Exception $exception) {
            throw new ProductException(ProductCodeEnum::ERR_PRODUCT_PACKAGE_DELETED, '', $exception);
        }
    }

    public static function getPackageProductListByIds($packageIds, $menuId)
    {
        return self::query()
                   ->where('menuId', $menuId)
                   ->whereIn('packageId', $packageIds)
                   ->get();
    }

    public function productRel()
    {
        return $this->hasOne(Product::class, 'id', 'productId');
    }

    public static function getPackageProductMappingByIds($packageIds)
    {
        return self::query()
                   ->with('productRel')
                   ->whereIn('packageId', $packageIds)
                   ->where('menuId', '=', 0)
                   ->get()
                   ->toArray();
    }

    public static function delPackageByMenuId($menuId)
    {
        try {
            $customerPackageIds = Product::query()
                                         ->whereIn(
                                             "id",
                                             PackageProductMapping::query()->where('menuId', $menuId)->pluck("packageId")
                                                                  ->toArray()
                                         )->where("kind", CodeEnum::KIND_CUSTOMER)->pluck("id")
                                         ->toArray();

            return self::query()
                       ->where('menuId', $menuId)
                       ->whereNotIn("packageId", $customerPackageIds)
                       ->delete();
        } catch (\Exception $exception) {
            throw new ProductException(ProductCodeEnum::ERR_PRODUCT_PACKAGE_DELETED, '', $exception);
        }
    }

}
